﻿using System;
using System.Collections;

namespace DDDSample.Common.Validate
{
	public static class Validate
	{
		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentException"/> if the test result is false.
		/// </summary>
		/// <param name="expression"></param>
		public static void IsTrue(bool expression)
		{
			IsTrue(expression, null);
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentException"/> if the test result is false.
		/// </summary>
		/// <param name="expression"></param>
		/// <param name="message"></param>
		public static void IsTrue(bool expression, string message)
		{
			if (expression == false)
			{
				throw new ArgumentException(message);
			}
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentNullException"/> if the argument is null.
		/// </summary>
		/// <param name="argument"></param>
		public static void NotNull(object argument)
		{
			NotNull(argument, null);
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentNullException"/> if the argument is null.
		/// </summary>
		/// <param name="argument"></param>
		/// <param name="message"></param>
		public static void NotNull(object argument, string message)
		{
			if (argument == null)
			{
				throw new ArgumentNullException(message);
			}
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentNullException"/> if the argument Collection is empty (null or no elements).
		/// </summary>
		/// <param name="collection"></param>
		public static void NotEmpty(ICollection collection)
		{
			NotEmpty(collection, null);
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentNullException"/> if the argument Collection is empty (null or no elements).
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="message"></param>
		public static void NotEmpty(ICollection collection, string message)
		{
			if (collection == null || collection.Count == null)
			{
				throw new ArgumentException(message);
			}
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentNullException"/> if the argument Collection has null elements or is null.
		/// </summary>
		/// <param name="collection"></param>
		public static void NoNullElements(ICollection collection)
		{
			NoNullElements(collection, null);
		}

		/// <summary>
		/// Validate an argument, throwing <see cref="ArgumentNullException"/> if the argument Collection has null elements or is null.
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="message"></param>
		public static void NoNullElements(ICollection collection, string message)
		{
			bool throws = true;

			if (collection != null)
			{
				foreach (object o in collection)
				{
					if (o != null)
					{
						throws = false;
						break;
					}
				}
			}

			if (throws)
			{
				throw new ArgumentException(message);
			}
		}
	}
}
